package ws_handler

import (
	"fmt"
	"peilian-api/app/global/databases/tables"
	"peilian-api/app/global/variable"
	"strconv"
)

func GetSessionObj(ID string) (*tables.Session, error) {
	sessionID, err := strconv.Atoi(ID)
	session := new(tables.Session)
	if err != nil {
		return session, err
		// panic("请传递整数session_id")
	}
	err = variable.DB.MySQL.Model(&tables.Session{}).Preload("User").Preload("Career").First(session, sessionID).Error
	if err != nil {
		return session, err
		// panic(fmt.Sprintf("场次[%d]不存在", sessionID))
	}
	return session, nil
}

func GetSpecialObj(Id string) (*tables.SpecialRecord, error) {
	specialRecord := new(tables.SpecialRecord)
	sid, err := strconv.Atoi(Id)
	if err != nil {
		return specialRecord, fmt.Errorf("id is error, info : %s", err)
	}

	err = variable.DB.MySQL.Model(&tables.SpecialRecord{}).Where("id = ?", sid).Preload("Question").First(specialRecord).Error
	if err != nil {
		return specialRecord, fmt.Errorf("db option error, sid : %d, info : %s", sid, err)
	}
	return specialRecord, nil
}
